package com.sgb.kjwl.viewmodel.enterprise;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.fragment.enterprise.ProfessionalQuoteFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProfessionalQuoteFrgModel {
    private ProfessionalQuoteFragment fragment;
    public String websiteNo = "";
    public String websiteType = "";
    private int pageNum = 1;
    IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");

    public ProfessionalQuoteFrgModel(ProfessionalQuoteFragment fragment) {
        this.fragment = fragment;
    }

    public void getProfessQuoteList(boolean isLoadMore, Map map) {
        if (!isLoadMore) {
            fragment.loading();
        }

        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.put("limit", 20);
        map.put("page", pageNum);
        if (!TextUtils.isEmpty(websiteType)&&!TextUtils.isEmpty(websiteNo)) {
            switch (Integer.parseInt(websiteType)) {
                case WebsiteInfoActivity.WEBSITE_FOR_PERSONAL:
                    map.put("userNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_TEAM:
                    map.put("teamNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_COMPANY:
                    map.put("enterpriseNo",websiteNo);
                    break;
            }
        }
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getOfferList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EpQuoteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Response<BaseEntity<ListResponse<List<EpQuoteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showQuoteList(response.body().getData().getList(), isLoadMore);
                    fragment.success();
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                    fragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EpQuoteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 获取服务城市
     */
    public void getServiceCity() {
        AddressHelper.getInstance().getAllArea(new AddressHelper.CallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                if (data != null && data.size() > 0) {
                    fragment.showCityPopWindow2(data);
                }
            }

            @Override
            public void threeData(List<AddressEntity> provinceList, List<List<AddressEntity>> cityList, List<List<List<AddressEntity>>> districtList) {

            }
        });
    }

    public void getPriceOptionByNo(int priceServiceNo) {
        NewAPIManager.getInstance().getEnterPrisenewAPI().getPriceOption(priceServiceNo).enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
                    fragment.showMakeupPopWindow(list1);

                } else {
                    MToast.showToast(fragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
}
